package com.sdau.soa.service.impl;

import com.sdau.soa.dao.NewsDao;
import com.sdau.soa.dao.NewsTypeDao;
import com.sdau.soa.service.INewsService;
import com.sdau.soa.vo.News;
import com.sdau.soa.vo.NewsType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.jws.WebMethod;
import javax.jws.WebService;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebService(
        targetNamespace = "com.sdau.soa",//定义命名空间
        serviceName = "WeatherWsService",//指定服务的名称
        name = "NewsService"//用于指定服务实现类的名称（指定portType的名称）
)
@Service
public class NewsServiceImpl implements INewsService {

    @Autowired
    private NewsTypeDao newsTypeDao;
    @Autowired
    private NewsDao newsDao;

    @Override
    @WebMethod
    public List getNews(int newsTypeId) {
        List<News> ln = newsDao.QueryByTypeId(newsTypeId);
        return ln;
    }

    @Override
    @WebMethod(exclude = true)
    public boolean InsertNews(News news) {
        int effectedNum = newsDao.InsertNews(news);
        if (effectedNum > 0)
            return true;
        return false;
    }

    @Override
    @WebMethod(exclude = true)
    public boolean UpdateNews(News news) {
        int effectedNum = newsDao.UpdateNews(news);
        if (effectedNum > 0)
            return true;
        return false;
    }

    @Override
    @WebMethod(exclude = true)
    public boolean DelNews(int newsId) {
        int effectedNum = newsDao.DelNews(newsId);
        if (effectedNum > 0)
            return true;
        return false;
    }

    @Override
    @WebMethod
    public List getNewsTypes() {
        List<NewsType> ln = newsTypeDao.queryAll();
        return ln;
    }


    @Override
    @WebMethod(exclude = true)
    public boolean InsertNewsType(NewsType news) {
        int effectedNum = newsTypeDao.insertType(news);
        if (effectedNum > 0)
            return true;
        return false;
    }

    @Override
    @WebMethod(exclude = true)
    public boolean UpdateNewsType(NewsType news) {
        int effectedNum = newsTypeDao.updateType(news);
        if (effectedNum > 0)
            return true;
        return false;
    }

    @Override
    @WebMethod(exclude = true)
    public boolean DelNewsType(int newsTypeId) {
        int effectedNum = newsTypeDao.delType(newsTypeId);
        if (effectedNum > 0)
            return true;
        return false;
    }

    @Override//按照条件查询
    @WebMethod(exclude = true)
    public List searchByInfo(String topic, Date EarlyTime, Date FinalTime) {
        Map<String,Object> map = new HashMap<String,Object>();

        String dateStr1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(EarlyTime);
        String dateStr2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(FinalTime);
        map.put("topic",topic);
        map.put("EarlyTime",dateStr1);
        map.put("FinalTime",dateStr2);
        List<News> ln = newsDao.QueryByInfo(map);
        return ln;
    }


}
